//
//  Date+Exs.swift
//  ttjx
//
//  Created by 文 on 2019/11/15.
//  Copyright © 2019 ttjx. All rights reserved.
//

import Foundation


extension Date{
    var timeStamp : String{
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let timeStamp = Int(timeInterval)
        return "\(timeStamp)"
    }
    
     /// 获取当前 毫秒级 时间戳 - 13位
    var timeStampMill : String {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let millisecond = CLongLong(round(timeInterval*1000))
        return "\(millisecond)"
    }
    
    var todayDate : String{
        let dateformatter = DateFormatter()

        dateformatter.dateFormat = "yyyy-MM-dd"// 自定义时间格式

        let time = dateformatter.string(from: Date())

        return time
    }
    //日期，斜杠
    var todayDateSlash : String{
        let dateformatter = DateFormatter()

        dateformatter.dateFormat = "yyyy/MM/dd"// 自定义时间格式

        let time = dateformatter.string(from: Date())

        return time
    }
    var todaySeconds : String{
        let dateformatter = DateFormatter()

        dateformatter.dateFormat = "yyyyMMddHHmmss"// 自定义时间格式

        let time = dateformatter.string(from: Date())

        return time
    }
    var currentMonth : String{
        let dateformatter = DateFormatter()

        dateformatter.dateFormat = "yyyy-MM"// 自定义时间格式

        let time = dateformatter.string(from: Date())

        return time
    }
    var currentYearNumber:String{
        let arr = currenYearMonth
        
        return arr[0] as? String ?? ""
    }
    var currentMonthNumber:String{
        let arr = currenYearMonth
        
        return arr[1] as? String ?? ""
    }
    var currenYearMonth : NSArray {
        let dateformatter = DateFormatter()

        dateformatter.dateFormat = "yyyy-MM"// 自定义时间格式

        let time = dateformatter.string(from: Date())
        
        let timstrings: [Substring] = time.split(separator: "-")
        var timeArrayStrings: [String] = []
        for item in timstrings {
            timeArrayStrings.append("\(item)")
        }

        return timeArrayStrings as NSArray
    }
    //比较两个日期间   相差多少天
    func daysBetweenDate(toDate: Date) -> Int {
        let components = Calendar.current.dateComponents([.day], from: self, to: toDate)
        return components.day ?? 0
    }
    func changeDateFormatter(dateString:String) -> String {
        let dateF = DateFormatter()
        dateF.dateFormat = "yyyyMMdd"
        
        if let date = dateF.date(from: dateString){
            let dateformatter = DateFormatter()
            dateformatter.dateFormat = "yyyy-MM-dd"// 自定义时间格式
            let time = dateformatter.string(from: date)
            return time
        }else{
            DLLog(message: "日期格式串  格式错误")
            return dateString
        }
    }
    func changeDateStringToDate(dateStr:String,formaStr:String? = "yyyy-MM-dd") -> Date {
        let formatter = DateFormatter()
        formatter.dateFormat = formaStr // 设置日期格式
        if let date = formatter.date(from: dateStr) {
            print("转换结果：\(date)")
            return date
        } else {
            print("无效的日期字符串")
            return Date()
        }
        
    }
    func timestampToDate(timestamp: TimeInterval, format: String? = "yyyy-MM-dd HH:mm:ss") -> String {
        let date = Date(timeIntervalSince1970: timestamp)
        let formatter = DateFormatter()
        formatter.dateFormat = format
        let dateString = formatter.string(from: date)
        return dateString
    }

    func judgeDateIsMin(minDate:String,maxDate:String,format: String? = "yyyy-MM-dd") -> Bool {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        if let date1 = dateFormatter.date(from: minDate),
           let date2 = dateFormatter.date(from: maxDate) {
            if date1 < date2 {
                print("\(date1) is earlier than \(date2)")
                return true
            } else if date1 > date2 {
                print("\(date1) is later than \(date2)")
                return false
            } else {
                print("\(date1) is equal to \(date2)")
                return true
            }
        }
        
        return false
    }
    /// 获取距离当前日期，几年几月几日的日期
    ///
    /// - Parameters:
    ///   - year: 距离当前日期的前/后几年  如：去年:-1   明年:1  今年:0
    ///   - month:  距离当前日期的前/后几个月  如：上个月:-1   下个月:1  这个月:0
    ///   - day:  距离当前日期的前/后几天 如：昨天:-1   明天:1   今天:0
    func currentDateToWantDate(year:Int,month:Int,day:Int)->Date {
        let current = Date()
        let calendar = Calendar(identifier: .gregorian)
        var comps:DateComponents?
        
        comps = calendar.dateComponents([.year,.month,.day], from: current)
        comps?.year = year
        comps?.month = month
        comps?.day = day
        return calendar.date(byAdding: comps!, to: current) ?? Date()
    }
    /*
     let yesterdayTime = Date.getLastDate(lastTimeInterval: -(24*60*60))
     let lastSevenTime = Date.getLastDate(lastTimeInterval: -(7*24*60*60))
     let lastMonthTime = Date.getLastDate(lastTimeInterval: -(30*24*60*60))
     */
    /// 输入要过去日期的时间，返回相应日期字符串
    static func getLastDate(lastTimeInterval: TimeInterval) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let lastTime: TimeInterval = lastTimeInterval // 往前减去往前时间的时间戳
        let lastDate = Date().addingTimeInterval(lastTime)
        let lastDay = dateFormatter.string(from: lastDate)
        return lastDay
    }
    func getLast30Days() -> String{
        return Date.getLastDate(lastTimeInterval: -(30*24*60*60))
    }
}
